﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace VatanComputerBot
{
    public class Bot
    {
        private static Bot botBase = null;

        public static Bot BotBase
        {
            get
            {
                if (botBase == null)
                    return new Bot();
                return botBase;
            }
            
        }
        private HtmlAgilityPack.HtmlDocument parser = new HtmlAgilityPack.HtmlDocument();
        public int GetProductCount(string link, string xPath)
        {
            List<Category> list = new List<Category>();
            parser.Load(@link);
            HtmlNodeCollection subNodes = parser.DocumentNode.SelectNodes(xPath);
            return int.Parse(subNodes.First().InnerText.Trim());
        }

        public List<Category> GetCategoryList(string link, string xPath)
        {
            List<Category> list = new List<Category>();
            parser.Load(@link);
            HtmlNodeCollection subNodes = parser.DocumentNode.SelectNodes(xPath);
            foreach (HtmlNode subNode in subNodes)
            {
                list.Add(new Category { name = subNode.InnerText.Split('&').First().Trim(), link = @subNode.Attributes["href"].Value.Trim() });
            }
            return list;
        }
        public List<ProductInfo> GetProductList(string link, string xPath)
        {
            List<ProductInfo> list = new List<ProductInfo>();
            parser.Load(@link);
            HtmlNodeCollection brandNames = parser.DocumentNode.SelectNodes(xPath+"/span");
            HtmlNodeCollection products = parser.DocumentNode.SelectNodes(xPath + "/a");
            if (products==null || brandNames==null||products.Count != brandNames.Count)  
                return null;
            for(int i=0;i<products.Count;i++)
            {
                list.Add(new ProductInfo { productName = products[i].InnerText.Trim(), link = products[i].Attributes["href"].Value.Trim(), brandName = brandNames[i].InnerText.Trim() });
            }
            return list;
        }
        public string GetInnerText(string link, string xPath)//Verilen dosyanın içindeki xpath'den içindeki değeri okur 
        {
            try
            {
                parser.Load(@link);
                HtmlNodeCollection list = parser.DocumentNode.SelectNodes(xPath);
                return list.First().InnerText.Trim();
            }
            catch 
            {
                return "";
            }
        }
        public string GetAttribute(string link, string xPath,string attribute)//Verilen dosyanın içindeki xpath'den özellik değerini okur 
        {
            try
            {
                parser.Load(@link);
                HtmlNodeCollection list = parser.DocumentNode.SelectNodes(xPath);
                return list.First().Attributes[attribute].Value.Trim();
            }
            catch 
            {
                return "";
            }
        }
        public string GetXPathOfInnerText(string link, string baseXPath ,string InnerText)
        {
            parser.Load(@link);
            HtmlNodeCollection list = parser.DocumentNode.SelectNodes(baseXPath);
            try
            {
                List<string> results = (from node in list
                                        where node.InnerText.Trim()==InnerText
                                        select node.XPath).ToList();
                if (results.Count == 1)
                    return results.First();
                return "";
            }
            catch
            {
                return "";
            }
        }
    }
}